package com.xxkk.gameCore.global.globalServer;

import com.xxkk.net.websocketNetty.Player;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xxkk
 * @version 1.0.0
 * @title ServerContext
 * @description 服务器所有模块数据存放
 * @create 2023/11/11 14:19
 **/
@Service
public class ServerContext {

    /**
     * 游戏有个区，区里有个小玩家，玩家酱去打水吗？（存放区玩家容器）
     */
    private final ConcurrentHashMap<Integer, GameAreaContext> gameAreaContextMap = new ConcurrentHashMap<>();
    /**
     * 保存连接到服务器但是没有登陆的客户端玩家
     */
    private final ConcurrentHashMap<Long, Player> connectingPlayerMap = new ConcurrentHashMap<>();

    public void init() { }
    public void addGameAreaContext(GameAreaContext gameAreaContext) {// 添加新区或者替换旧区
        this.gameAreaContextMap.put(gameAreaContext.getAreaId(), gameAreaContext);
    }

    public GameAreaContext getGameAreaContext(int areaId) {
        return this.gameAreaContextMap.get(areaId);
    }
    public ConcurrentHashMap<Integer, GameAreaContext> getGameAreaContextMap() {
        return gameAreaContextMap;
    }
    public ConcurrentHashMap<Long, Player> getConnectingPlayerMap() {
        return connectingPlayerMap;
    }
    public boolean rmConnectingPlayer(Player player){// 移除连接状态中的用户
        return this.connectingPlayerMap.remove(player.getPlayerId()) != null;
    }
    public Player addConnectingPlayer(Player player){// 添加连接状态中的用户
        return this.connectingPlayerMap.put(player.getPlayerId(),player);
    }

}
